let utils = require('../../utils/utils');
let util = require('../../utils/util');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    homestayId: '',
    homestayMsg: [],
    startDate: '',
    endDate: '',
    freeTicket: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(e) {
    let homestayId = e.homestayId;
    this.setData({
      homestayId
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getHomestayMsg(), this.getFreeTicket();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },

  // 获取酒店订单详情
  getHomestayMsg() {
    let that = this;
    let id = that.data.homestayId;
    utils.request({
      url: 'order/hotelOrderInfo',
      data: {
        id
      },
      success: function(res) {
        let r = res.data;
        if (r.success) {
          let data = r.data;
          let endDate = util.turnDate(data.endDate);
          let startDate = util.turnDate(data.startDate);
          that.setData({
            homestayMsg: data,
            startDate,
            endDate
          })
        }
      }
    })
  },

  // 获取赠票信息
  getFreeTicket() {
    let that = this;
    let roomId = that.data.homestayId;
    utils.request({
      url: 'hotel/giveTicketInfo',
      data: {
        roomId
      },
      success: function(res) {
        let r = res.data;
        if (r.success) {
          let data = r.data;
          that.setData({
            freeTicket: data
          })
        }
      }
    })
  },

})